package com.example.martian.utils;

import io.github.novacrypto.bip39.MnemonicGenerator;
import io.github.novacrypto.bip39.SeedCalculator;
import io.github.novacrypto.bip39.Words;
import io.github.novacrypto.bip39.wordlists.English;
import io.github.novacrypto.hashing.Sha256;
import org.springframework.stereotype.Component;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.Keys;

import java.security.SecureRandom;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Component
public class CreatWallet {

    public Object nem(String pwd) {
        Map resultMap = new LinkedHashMap();
        StringBuilder sb = new StringBuilder();
        byte[] entropy = new byte[Words.TWELVE.byteLength()];
        new SecureRandom().nextBytes(entropy);
        new MnemonicGenerator(English.INSTANCE)
                .createMnemonic(entropy, sb::append);
        String mnemonic = sb.toString();
        System.out.println("mnemonic:" + mnemonic);
        List mnemonicList = Arrays.asList(mnemonic.split(" "));
        byte[] seed = new SeedCalculator()
                .withWordsFromWordList(English.INSTANCE)
                .calculateSeed(mnemonicList, pwd);
        ECKeyPair ecKeyPair = ECKeyPair.create(Sha256.sha256(seed));
        String privateKey = ecKeyPair.getPrivateKey().toString(16);
        String publicKey = "0x"+ ecKeyPair.getPublicKey().toString(16);
        String address = "0x"+ Keys.getAddress(publicKey);
        resultMap.put("privateKey",privateKey);
        resultMap.put("publicKey",publicKey);
        resultMap.put("address",address);
        System.out.println("privateKey:" + privateKey);
        System.out.println("publicKey:" +publicKey);
        System.out.println("address:" +address);
        return resultMap;
    }
    /**
     * 创建账户
     *
     * @param pwd
     * @return mnemonic :助记词
     * privateKey：私钥
     * publicKey：公钥
     * address：地址
     * accountFilePath：账户文件路径
     */
//    public Map createAccount(String pwd) {
//        Map resultMap = new LinkedHashMap();
//        //String filePath = System.getProperty("user.home") + File.separator + "keystore";
//        StringBuilder sb = new StringBuilder();
//        byte[] entropy = new byte[Words.TWELVE.byteLength()];
//        new SecureRandom().nextBytes(entropy);
//        new MnemonicGenerator(English.INSTANCE)
//                .createMnemonic(entropy, sb::append);
//        String mnemonic = sb.toString();
//        System.out.println("mnemonic:" + mnemonic);
//        List mnemonicList = Arrays.asList(mnemonic.split(" "));
//        byte[] seed = new SeedCalculator()
//                .withWordsFromWordList(English.INSTANCE)
//                .calculateSeed(mnemonicList, pwd);
//        ECKeyPair ecKeyPair = ECKeyPair.create(Sha256.sha256(seed));
//        String privateKey = ecKeyPair.getPrivateKey().toString(16);
//        String publicKey = ecKeyPair.getPublicKey().toString(16);
//        String address = "0x" + Keys.getAddress(publicKey);
//        //创建钱包地址与密钥
//        String fileName = null;
//        try {
//            fileName = WalletUtils.generateWalletFile(pwd, ecKeyPair, new File(filePath), false);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        if (fileName == null) {
//            return null;
//        }
//        String accountFilePath = filePath + File.separator + fileName;
//        resultMap.put("mnemonic", mnemonic);
//        resultMap.put("privateKey", privateKey);
//        resultMap.put("publicKey", publicKey);
//        resultMap.put("address", address);
//        resultMap.put("accountFilePath", accountFilePath);
//        return resultMap;
    }



